import com.sun.corba.se.impl.orbutil.StackImpl;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class CreateMaze {
    public static int x, y, nx, ny, dir;//x、y为当前单元坐标，nx、ny为相邻单元的坐标，dir为当前单元的墙的方向

    public static <IArrayStack> void initialize(int mx, int my) {
        MazeUnit[][] pos = new MazeUnit[mx][my];//mx、my为行数、列数加2
        //初始化迷宫单元数组
        int i, j;
        for (i = 0; i < mx; i++) {
            for (j = 0; j < my; j++) {
                int[] wall = new int[4];
                for (int w = 0; w < 4; w++) {
                    wall[w] = 1;//将所有迷宫单元用墙隔开
                }
                pos[i][j] = new MazeUnit(false, wall, true);
                pos[i][j].x = i;
                pos[i][j].y = j;
            }
        }

        //设置可访问性数组的值，创建外围边界
        for (i = 1; i < mx - 1; i++) {
            for (j = 1; j < my - 1; j++) {
                pos[i][j].accessible = true;
            }
        }
        i = 0;
        for (j = 0; j < my; j++) {
            pos[i][j].accessible = false;
        }
        i = mx - 1;
        for (j = 0; j < my; j++) {
            pos[i][j].accessible = false;
        }
        j = 0;
        for (i = 0; i < mx; i++) {
            pos[i][j].accessible = false;
        }
        j = my - 1;
        for (i = 0; i < mx; i++) {
            pos[i][j].accessible = false;
        }

        //生成入口和两个随机出口
        Random randx = new Random();
        int endx = randx.nextInt(mx - 2) + 1;
        Random randy = new Random();
        int endy = randy.nextInt(my - 2) + 1;
        pos[1][1].wall[0] = 0;
        pos[endx][my - 2].wall[2] = 0;
        pos[mx - 2][endy].wall[3] = 0;

        IArrayStack path = new StackImpl(mx - 2, my - 2);
        path.push(pos[1][1]);

        x = y = 1;
        nx = ny = 0;
        dir = 0;
        Object[] pathx = new Object[(mx - 2) * (my - 2)];
        Object[] pathy = new Object[(mx - 2) * (my - 2)];
        while (countIsVisited(pos, mx, my) < (mx - 2) * (my - 2)) {
            randway();
            //记录两个出口的路径
            if ((x == endx) && (y == my - 2)) {
                pathx = path.copyArray().clone();
            }
            if ((x == mx - 2) && (y == endy)) {
                pathy = path.copyArray().clone();
            }

            if (isBlock(pos, x, y) == false) {
                breakwall(pos, path);
            } else if (isBlock(pos, x, y) == true) {
                pos[x][y].isVisited = true;
                path.pop();//将当前单元出栈
                int[] tempwall = new int[4];
                MazeUnit temp = new MazeUnit(false, tempwall, true);
                temp = (MazeUnit) path.top();//将前一单元转换为当前单元
                x = temp.x;
                y = temp.y;//将坐标转换为前一单元的坐标

            }
        }

        JFrame win2 = new JFrame();
        win2.setTitle("随机生成迷宫");
        win2.setSize(mx * 30 + 80, my * 30 + 80);
        win2.setLocationRelativeTo(null);
        win2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //绘制迷宫的图片
        drawMaze pic = new drawMaze(mx, my);
        pic.getData(pos, pathx, pathy, mx, my);
        win2.add(pic, BorderLayout.CENTER);

        win2.setResizable(false);
        win2.setVisible(true);
    }

}
